package cn.bm.web;

import cn.bm.web.controllers.RouteMain;
import cn.bm.web.db.DBConfig;

import static spark.Spark.*;

public class Main {
    public static void main(String[] args) {
        int n = 8090;
        port(n);
        staticFiles.location("/public");

        before("/*",(request, response) -> {
            response.header("Access-Control-Allow-Origin", "*");
            response.header("Cache-Control", "no-cache");
            response.header("Access-Control-Allow-Headers","*");
            response.header("Access-Control-Request-Method","*");
            response.type("text/html");
            request.raw().setCharacterEncoding("UTF-8");
        });

       try {
           DBConfig.getConnection();
           System.out.println("数据库连接成功，程序继续执行...");
           // 初始化各route
           RouteMain.InitSiteRoutes();
       }catch (RuntimeException e) {
           e.printStackTrace();
           System.err.println("数据库连接失败，程序退出...");
           System.exit(1); // 数据库连接失败，退出程序
       }
    }
}